using System.Collections.Generic;
using System.Text;

namespace OracleInternal.SqlAndPlsqlParser.LocalParsing
{
	internal class OracleLpQueryBlock : OracleLpStatementDataContainer
	{
		protected OracleLpWithClause m_vWithClause;

		protected OracleLpSelectClause m_vSelectClause;

		protected OracleLpFromClauseBase m_vFromClause;

		protected bool m_vIsFromDual;

		internal override OracleLpStatementElementType ElementType => OracleLpStatementElementType.QueryBlock;

		public OracleLpWithClause WithClause
		{
			get
			{
				return m_vWithClause;
			}
			set
			{
				m_vWithClause = value;
				if (m_vWithClause != null)
				{
					m_vWithClause.Parent = this;
				}
			}
		}

		public OracleLpSelectClause SelectClause => m_vSelectClause;

		public OracleLpFromClauseBase FromClause
		{
			get
			{
				return m_vFromClause;
			}
			set
			{
				m_vFromClause = value;
				if (m_vFromClause != null)
				{
					m_vFromClause.Parent = this;
				}
			}
		}

		public bool IsFromDual => m_vIsFromDual;

		public override List<OracleLpColumnDescriptor> ColumnDescriptors => m_vSelectClause.ColumnDescriptors;

		public OracleLpQueryBlock(OracleLpSubquery parentSubquery)
			: base(parentSubquery)
		{
			m_vSelectClause = new OracleLpSelectClause(this);
		}

		public override void RetrieveNamedObjectReferences(OracleLpStatement statement)
		{
			if (m_vWithClause != null)
			{
				m_vWithClause.RetrieveNamedObjectReferences(statement);
			}
			foreach (OracleLpSelectTerm select in m_vSelectClause.SelectList)
			{
				if (select.Type == OracleLpSelectTermType.SPECIFIC)
				{
					OracleLpExpression expression = ((OracleLpSelectTermSpecific)select).Expression;
					if (expression.ExpressionType == OracleLpExpressionType.SCALAR_SUBQUERY_EXPRESSION)
					{
						((OracleLpScalarSubqueryExpression)expression).RetrieveNamedObjectReferences(statement);
					}
				}
			}
			foreach (OracleLpStatementDataContainer term in m_vFromClause.Terms)
			{
				term.RetrieveNamedObjectReferences(statement);
			}
		}

		internal override void ToString(StringBuilder sb)
		{
			if (m_vWithClause != null)
			{
				m_vWithClause.ToString(sb);
			}
			m_vSelectClause.ToString(sb);
			m_vFromClause.ToString(sb);
		}
	}
}
